package cae.appBar
{
	import mx.containers.HBox;
	import mx.collections.ArrayCollection;
	import mx.controls.Button;
	import flash.events.MouseEvent;
	import mx.controls.Alert;
	import flash.display.DisplayObject;
	import mx.controls.ToggleButtonBar;
	import flash.events.Event;
	import mx.containers.ApplicationControlBar;
	import mx.controls.Spacer;
	import cae.windows.CaeWindowEvent;
	import cae.appBar.buttons.CaeMenuButton;
	import cae.appBar.buttons.CaePopUpMenuButton;
	import cae.appBar.buttons.HistoryButton;
	import mx.events.FlexEvent;
	
	[Event(name="OPEN",type="cae.windows.CaeWindowEvent")]
	
	public class CaePopUpMenuBar extends ApplicationControlBar
	{
		public var _dataProvider:XML;
		
		public function CaePopUpMenuBar()
		{
			styleName = "applicationBarStyle";
			percentWidth = 100;
		}
		
		override protected function createChildren():void
		{
			super.createChildren();
		}
		
		public function set dataProvider(dp:XML):void
		{
			_dataProvider = dp;
			
			removeAllChildren();
			
			for each(var o:XML in dataProvider.children())
			{
				if( o.name() == "botonDirecto")
				{
					var bo:CaeMenuButton = new CaeMenuButton();
					bo.label = o.@label;
					bo.dataProvider = o[0];
					bo.addEventListener(CaeWindowEvent.OPEN, seleccion);
					addChild(bo);
				}
				else
				{
					var b:CaePopUpMenuButton = new CaePopUpMenuButton();
					b.label = o.@label;
					b.dataProvider = o[0];
					b.addEventListener(CaeWindowEvent.OPEN, seleccion);
					addChild(b);
				}
				

			}
			
			var s:Spacer = new Spacer();
			s.percentWidth = 100;
			addChild(s);
			
			var foot:HistoryButton = new HistoryButton();
			foot.addEventListener(MouseEvent.CLICK, showPath);
			addChild(foot);
		}
		
		private function showPath(e:MouseEvent):void
		{
			dispatchEvent(new Event("SHOW_PATH"));
		}
		
		public function get dataProvider():XML
		{
			return _dataProvider;
		}
		
		public function seleccion(e:CaeWindowEvent):void
		{
			for each( var o:Object in getChildren() )
			{
				if(o is CaeISelectableButton)
					CaeISelectableButton(o).selected = false;
			}
			
			if(e.target is CaeISelectableButton)
					CaeISelectableButton(e.target).selected = true;
		}
	}
}